﻿using System;
using System.Data;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using AnyOffice.Components;
using AnyOffice.DAL;
using Microsoft.ApplicationBlocks.Data;

namespace AnyOffice.general.attend.manage
{
    public partial class export_cc : ModulePage
    {

        protected void Page_Load(object sender, EventArgs e)
        {
            DateTime time = (Request.QueryString["date1"] != null) ? Convert.ToDateTime(Request.QueryString["date1"].ToString()) : DateTime.Now;
            DateTime time2 = (Request.QueryString["date2"] != null) ? Convert.ToDateTime(Request.QueryString["date2"].ToString()) : DateTime.Now;
            DateTime time3 = Convert.ToDateTime(time.ToString("yyyy-MM-dd") + " 00:00:00");
            DateTime time4 = Convert.ToDateTime(time2.ToString("yyyy-MM-dd") + " 23:59:59");
            Response.Clear();
            Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode("考勤出差数据.csv"));
            Response.Write("部门,姓名,出差地点,开始日期,结束日期,批示领导,状态\n");
            string commandText = "SELECT [Attend_Evection].[EvectionId],[Attend_Evection].[EvectionDest],[Attend_Evection].[EvectionDate1],[Attend_Evection].[EvectionDate2],[Attend_Evection].[Status],A.[UserName] AS [LeaderName],B.[UserName],[Department].[DeptName] FROM [dbo].[Attend_Evection],[dbo].[Users] AS A,[dbo].[Users] AS B,[dbo].[Department] WHERE [Attend_Evection].[Allow] = 1 AND [Attend_Evection].[LeaderId] = A.[UserId] AND [Attend_Evection].[UserId] = B.[UserId] AND B.[DeptId] = [Department].[DeptId] AND (([Attend_Evection].[EvectionDate1] >= '" + time3.ToString() + "' AND [Attend_Evection].[EvectionDate1] <= '" + time4.ToString() + "') OR ([Attend_Evection].[EvectionDate2] >= '" + time3.ToString() + "' AND [Attend_Evection].[EvectionDate2] <= '" + time4.ToString() + "')) ORDER BY [Attend_Evection].[EvectionId] ASC";
            DataTable table = SqlHelper.ExecuteDataset(base.Conn, CommandType.Text, commandText).Tables[0];
            if (table.Rows.Count > 0)
            {
                for (int i = 0; i < table.Rows.Count; i++)
                {
                    Response.Write(table.Rows[i]["DeptName"].ToString() + ",");
                    Response.Write(table.Rows[i]["UserName"].ToString() + ",");
                    if (table.Rows[i]["EvectionDest"] != DBNull.Value)
                    {
                        Response.Write(table.Rows[i]["EvectionDest"].ToString());
                    }
                    Response.Write(",");
                    Response.Write(Convert.ToDateTime(table.Rows[i]["EvectionDate1"].ToString()).ToString("yyyy-MM-dd") + ",");
                    Response.Write(Convert.ToDateTime(table.Rows[i]["EvectionDate2"].ToString()).ToString("yyyy-MM-dd") + ",");
                    Response.Write(table.Rows[i]["LeaderName"].ToString() + ",");
                    if (Convert.ToInt32(table.Rows[i]["Status"].ToString()) == 0)
                    {
                        Response.Write("在外");
                    }
                    else
                    {
                        Response.Write("归来");
                    }
                    Response.Write("\n");
                }
            }
        } 

    }
}
